﻿using System.Threading;
using System.Windows.Automation;
using System.Windows.Media;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace WPFDemo.SampleTests
{
    [TestClass]
    public class SampleTests
    {
        public const string LOCAL_EXE_LOCATION = @"..\..\..\WPFDemo\bin\Debug\WPFDemo.exe";

        public const int DURATION_TO_WAIT_FOR_EXPAND_ANIMATION = 1000;
        public const int DURATION_TO_WAIT_FOR_COLLAPSE_ANIMATION = 800;

        private AutomationElement _window = null;

        [ClassInitialize()]
        public static void ClassInitialize(TestContext context)
        {
            // Warm up the application; initialize WPF
            AutomationElement window = null;

            window = UIAutomationHelper.LaunchApplication(LOCAL_EXE_LOCATION);

            Thread.Sleep(1000);

            if (window != null)
                (window.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern).Close();
        }

        [TestInitialize()]
        public void MyTestInitialize()
        {
            Thread.Sleep(1000);

            _window = UIAutomationHelper.LaunchApplication(LOCAL_EXE_LOCATION);
                        
            Thread.Sleep(2000);
        }


        [TestCleanup()]
        public void MyTestCleanup()
        {
            Thread.Sleep(1000);

            if (_window != null)
                (_window.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern).Close();
        }


        [TestMethod]
        public void TestScalingModal()
        {
            TestExpandCollapse();

            int numberAnimations = UIAutomationHelper.GetNumberOfChildElements(_window, "AnimationSelectionComboBox");
            for (int i = 0; i < numberAnimations; i++)
            {
                UIAutomationHelper.SelectIndexFromDropdown(_window, "AnimationSelectionComboBox", i);
                TestExpandCollapse();
            }            
        }

        private void TestExpandCollapse()
        {
            UIAutomationHelper.InvokeElementById(_window, "ExpandButton");
            Thread.Sleep(DURATION_TO_WAIT_FOR_EXPAND_ANIMATION);
            Assert.IsFalse(UIAutomationHelper.ElementIsEnabled(_window, "ExpandButton"), "The background was not disabled on Expand()");
            Assert.IsTrue(UIAutomationHelper.ElementExists(_window, "CollapseButton"), "The pop-up was not displayed on Expand()");
            UIAutomationHelper.InvokeElementById(_window, "CollapseButton");
            Thread.Sleep(DURATION_TO_WAIT_FOR_COLLAPSE_ANIMATION);
            Assert.IsTrue(UIAutomationHelper.ElementDoesNotExist(_window, "CollapseButton"), "The pop-up was not hidden on Collapse()");
            Assert.IsTrue(UIAutomationHelper.ElementIsEnabled(_window, "ExpandButton"), "The background was not re-enabled on Collapse()");
        }





    }
}
